#include "src/mainwindow.h"
#include "ui_mainwindow.h"
#include <QSettings>
#include <QScreen>
#include <QGuiApplication>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),ui(new Ui::MainWindow),start_menu_(nullptr)
{
    ui->setupUi(this);
    ui->verticalLayout->setContentsMargins(0,0,0,0);

    create_widget();

    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setWindowFlag(Qt::FramelessWindowHint);

    ui->graphicsView->showFullScreen();

    QMap<QString, QString> tmp_map;
    tmp_map.insert("sleeping",":/img_res/sleeping.gif");
    loading_dlg_ = UiAnimatedDialog::CreateCustomDialog(QString("对话框"),QString("大魔王正在睡觉，对你的到来没有任何反应"),\
                                                                 tmp_map,false,UiAnimatedDialog::ENUM_AVATAR_LOOP);
}

MainWindow::~MainWindow()
{
    delete ui;

    if(icon_thread_ != nullptr && icon_thread_->isRunning())
    {
        icon_thread_->wait();
    }
    delete icon_thread_;
    icon_thread_ = nullptr;
}

void MainWindow::create_widget()
{
    ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    //获取显示器对象，用来控制背景图片和开始菜单展示
    QScreen* screen_ = QGuiApplication::primaryScreen();

    //获取桌面背景
    QSettings wall_paper("HKEY_CURRENT_USER\\Control Panel\\Desktop", QSettings::NativeFormat);
    QString wall_paper_path = wall_paper.value("Wallpaper").toString();

    //将图片设置为显示器大小
    wallpaper_img_.load(wall_paper_path);
    wallpaper_img_ = wallpaper_img_.scaled(screen_->size(),Qt::KeepAspectRatioByExpanding,Qt::SmoothTransformation);
    QRect rect(wallpaper_img_.rect().topLeft().x(),wallpaper_img_.rect().topLeft().y() + (wallpaper_img_.rect().height() - screen_->geometry().height())/2,screen_->size().width() - 1.5,screen_->size().height()-1.5);
    QImage img = wallpaper_img_.copy(rect);

    //计算开始菜单位置和大小
    QRectF available_rect = screen_->availableGeometry();
    QRectF real_rect = screen_->geometry();
    QRectF start_menu_pos_rect;
    int    start_menu_pos;

    if(available_rect.topLeft() == QPointF(0,0))
    {
        //如果可用矩形的左上点是(0,0)，证明开始菜单在右边或者下边
        if(available_rect.height() < real_rect.height())
        {
            //高度小代表任务栏在下面QRectF(0,824 1536x40)
            start_menu_pos_rect = QRectF(QPointF(available_rect.bottomLeft()),QSize(available_rect.width(),real_rect.height() - available_rect.height()));
            start_menu_pos = ENUM_BOTTOM;
        }
        else if(available_rect.width() < real_rect.width())
        {
            //右边QRectF(1462,0 74x864)
            start_menu_pos_rect = QRectF(QPointF(available_rect.topRight()),QSize(real_rect.width() - available_rect.width(),available_rect.height()));
            start_menu_pos = ENUM_RIGHT;
        }
    }
    else
    {
        //其他情况说明开始菜单在上边或者左边
        if(available_rect.height() < real_rect.height())
        {
            //高度小代表任务栏在上边QRectF(0,0 1536x40)
            start_menu_pos_rect = QRectF(QPointF(0,0),QSize(available_rect.width(),real_rect.height() - available_rect.height()));
            start_menu_pos = ENUM_TOP;
        }
        else if(available_rect.width() < real_rect.width())
        {
            //左边QRectF(0,0 74x864)
            start_menu_pos_rect = QRectF(QPointF(0,0),QSize(real_rect.width() - available_rect.width(),available_rect.height()));
            start_menu_pos = ENUM_LEFT;
        }
    }

    //构造开始菜单对象
    QSettings ini_settings(QString("%1//config//config.ini").arg(qApp->applicationDirPath()),QSettings::IniFormat);
    int system_style = ini_settings.value("GLOBAL_SETTINGS/Current_Style").toInt();
    start_menu_ = new UiStartMenu(start_menu_pos_rect,system_style);

    //展示桌面背景
    scene_ = new UiGraphicsScene(img,this);
    ui->graphicsView->setScene(scene_);

    scene_->addItem(start_menu_);
}

void MainWindow::Init()
{
    loading_dlg_->Show("sleeping",false);
    //开线程获取桌面图标
    icon_thread_ = new ThreadGetDesktopIcon(this);
    connect(icon_thread_,SIGNAL(finished()),this,SLOT(slot_get_icon_finished()));
    icon_thread_->start();
}

void MainWindow::slot_get_icon_finished()
{
    desktop_icon_list_ = icon_thread_->GetResult();

    //将图标展示在scene上
    QList<DesktopIcon>::iterator icon_iter = desktop_icon_list_.begin();
    QScreen* screen_ = QGuiApplication::primaryScreen();

    int row=0,column=0;
    double row_height = screen_->availableSize().height() / 10.0;

    while(icon_iter != desktop_icon_list_.end())
    {
        scene_->addItem(&(*icon_iter));
        (*icon_iter).setPos(QPointF(column*80,row*row_height));
        ++row;

        if(row*row_height >= screen_->availableSize().height())
        {
            ++column;
            row = 0;
        }
        if(column*80 >= screen_->availableSize().width())
        {
            break;
        }

        ++icon_iter;
    }
    loading_dlg_->close();
}

